package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.roleBo.RoleBo2;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.Name;
import java.util.Date;
import java.util.List;

/***
 * title CouponServiceImpl
 * @description <TODO description class purpose>
 * @author ZCZ
 * @version 1.0.0
 * @create 2022/9/9 13:29
 **/
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired

    MarketCouponUserMapper marketCouponUserMapper;
    @Override
    public CommonData<MarketCoupon> getAllInfo(BasePageInfo info, String name, Short type, Short status) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCouponExample example = new MarketCouponExample();
        example.setDistinct(true);
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketCouponExample.Criteria criteria = example.createCriteria();

        //判断是否过期来改变status
        List<MarketCoupon> marketCouponList1=marketCouponMapper.selectByExample(example);
        for (MarketCoupon marketCoupon : marketCouponList1) {
            if(marketCoupon.getTimeType()!=0) {
                if (marketCoupon.getEndTime() == null) {
                    marketCoupon.setStatus((short) 2);
                    marketCouponMapper.updateByPrimaryKey(marketCoupon);
                } else if (marketCoupon.getEndTime().getTime() < new Date().getTime()) {
                    marketCoupon.setStatus((short) 2);
                    marketCouponMapper.updateByPrimaryKey(marketCoupon);
                }
            }
        }

        criteria.andDeletedEqualTo(false);
        // 条件查询
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }

        List<MarketCoupon> marketCouponList = marketCouponMapper.selectByExample(example);
        // for (MarketCoupon marketCoupon : marketCouponList) {
        //     if(marketCoupon.getStatus()==2){
        //         marketCoupon.setStatus((short)3);
        //     }
        //     if(marketCoupon.getStatus()==3){
        //         marketCoupon.setStatus((short)2);
        //     }
        // }
        CommonData<MarketCoupon> marketCouponCommonData = new CommonData<>();
        marketCouponCommonData.setLimit(info.getLimit());
        marketCouponCommonData.setPage(info.getPage());

        PageInfo pageInfo = new PageInfo(marketCouponList);
        Integer total = (int) pageInfo.getTotal();
        int pages = pageInfo.getPages();
        marketCouponCommonData.setPage(pages);
        marketCouponCommonData.setTotal(total);
        marketCouponCommonData.setList(marketCouponList);

        return marketCouponCommonData;

    }

    @Override
    public MarketCoupon create(MarketCoupon marketCoupon) {
        Date date = new Date();
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);

        //基于领取时间的有效天数来生成startTime和endTime
        // if(marketCoupon.getTimeType()==0){
        //     marketCoupon.setStartTime(date);
        //     int days = marketCoupon.getDays();
        //     Date date1 = new Date();
        //     date1.setTime(date.getTime() + days*1000*60*60*24);
        //     // date.setTime(date.getTime() + days*1000*60*60*24);
        //     marketCoupon.setEndTime(date1);
        // }
        marketCouponMapper.insertSelective(marketCoupon);
        MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(marketCoupon.getId());
        return marketCoupon1;
    }

    @Override
    public void delete(MarketCoupon marketCoupon) {
        Date date = new Date();
        marketCoupon.setUpdateTime(date);
        marketCouponMapper.deleteByPrimaryKeyLogic(marketCoupon.getId());
    }

    @Override
    public MarketCoupon update(MarketCoupon marketCoupon) {
        // Date date = new Date();
        // marketCoupon.setUpdateTime(date);
        marketCouponMapper.updateByPrimaryKey(marketCoupon);
        MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(marketCoupon.getId());
        return marketCoupon1;
    }

    @Override
    public MarketCoupon read(int id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }

    @Override
    public CommonData<MarketCouponUser> getMarketCouponUser(BasePageInfo info,int couponId,Integer userId,Short status) {
        PageHelper.startPage(info.getPage(),info.getLimit());
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setDistinct(false);
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status!=null){
            // if(status==2){
            //     status=3;
            // }
            // if(status==3){
            //     status=2;
            // }
            criteria.andStatusEqualTo(status);
        }

        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(example);
        CommonData<MarketCouponUser> marketCouponUserCommonData = new CommonData<>();
        for (MarketCouponUser marketCouponUser : marketCouponUserList) {
            if(marketCouponUser.getStatus()==2){
                marketCouponUser.setStatus((short)3);
            }
            else if(marketCouponUser.getStatus()==3){
                marketCouponUser.setStatus((short)2);
            }
        }
        marketCouponUserCommonData.setLimit(info.getLimit());
        marketCouponUserCommonData.setPage(info.getPage());
        PageInfo pageInfo = new PageInfo(marketCouponUserList);
        int pages = pageInfo.getPages();
        int total = (int) pageInfo.getTotal();
        marketCouponUserCommonData.setPages(pages);
        marketCouponUserCommonData.setTotal(total);
        marketCouponUserCommonData.setList(marketCouponUserList);

        return  marketCouponUserCommonData;
    }
}
